#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#define bool int

typedef struct part part;

bool is_prime(int n);
int read_line(char str[],int n);

void control(void);
void create(part** newadd);
void search(part* sc,int aim);
void print(part* pri);
void outputSEARCH(part* print);
void change(part* pri);
void searchchange(part* sc,int aim);


int numofpart = 0;

struct part{
	int number;
	char name[80];
	int quantity;
	part* next;
};//This part is for case 7 to use

int main(void)
{
  int choice=1;
  while (1)
  {
  	printf("0.Exit\n"\
	  "1.SumEnteredNumbers\n"\
	  "2.CheckRepearedDigits\n"\
	  "3.ComputeCompoundInterest\n"\
	  "4.CheckIfPrime\n"\
	  "5.RecursiveXpowN\n"\
	  "6.PrintMonthReminderList\n"\
	  "7.InsertSearchUpdatePrintParts\n");
    scanf("%d",&choice);
	if (choice < 0 || choice > 7){
		printf("Please choose the right option between 0 and 7\n"); 
		continue;
	}
	switch (choice)
    {
    case 0:  //Exit
    exit(0);
	case 1:  //SumEnteredNumbers
	{
     	char input[80];
     	char *p = input;
     	int sum = 0;
     	scanf("%s",input);
     	for(int i = 0; i < strlen(input); i++){
     		if(*(p + i) == '0') break;
     		sum += ((int)(*(p + i)) - 48);
		 }
     	printf("%d\n",sum);
		printf("1.SumEnteredNumbers is completed\n");
		break;
	}
	case 2: //CheckRepeatedDigits
	{  
	    getchar();
	 	char nums[80];
	 	char *num = nums;
	 	int flag = 0;
	 	scanf("%s",nums);
	 	for(int i = 0; i < strlen(nums); i++){
	 		for(int j = 0; j < strlen(nums); j++){
	 			if(*(num + i) == nums[j]) {
	 				if((num + i) != &nums[j]) flag = 1;
				 }
			 }
		 }
	 	if(flag == 1) printf("有重复\n");
	 	else printf("没有重复\n");
		printf("2.CheckRepeatedDigits is completed\n");
		break;
	}
	case 3: //ComputeCompoundInterest
	{
	 	double rate = 0;
	 	int year = 0;
	 	printf("\nEnter initial interest rate: ");
	 	scanf("%lf",&rate);
	 	printf("\nEnter number of years: ");
	 	scanf("%d",&year);
		double money[year][5];
		for(int i = 0; i < year; i++){
			for(int j = 0; j < 5; j++){
				money[i][j] = 100.0 * pow((1 + (rate + j) / 100.0),(i + 1));
			}
		} 
		printf("多年\t");
		for(int i = 0; i < 5; i++){
			printf("%.0lf%%\t",rate + i);
		}
		printf("\n");
		for(int i = 0; i < year; i++){
			printf("%d\t",i + 1);
			for(int j = 0; j < 5; j++){
				printf("%.2lf\t",money[i][j]);
			}
			printf("\n");
		}
		printf("3.ComputeCompoundInterest is completed\n");
		break;
    }
	case 4:  //CheckIfPrime
	{
	 	int num;
	 	printf("Enter a number: ");
	 	scanf("%d",&num);
	 	if(is_prime(num) == 1) printf("Not prime\n");
	 	else printf("Prime\n");
	 	printf("4.CheckIfPrime is completed\n\n");
		break;
	}
	case 5: //RecursiveXpowN
	{
	 	int x,n,output=1;
	 	printf("\nEnter x: ");
	 	scanf("%d",&x);
	 	printf("\nEnter n: ");
	 	scanf("%d",&n);
		for(int i=1;i<=n;i++) output=output*x;
        printf("\n%d power %d = %d\n",x,n,output);
        printf("5.RecursiveXpowN is completed\n\n");
		break;
	}
	case 6: //PrintMonthReminderList
	{
	 	char reminders[100][100];
	 	char day_str[3],msg_str[101];
	 	int day,i,j,num_remind = 0;
	 	while(1) 
	 	{
	 		if (num_remind == 100)
	 		{
	 			printf("-- NO SPACE LEFT --\n");
			}
		    
		    printf("\nEnter date and reminders: ");
			scanf("%2d",day);
		    sprintf(day_str,"%2d",day);
		    read_line(msg_str,100);
		    
		    for (i = 0; i < num_remind; i++)
		     if ( strcmp(day_str,reminders[i])< 0)
		     break;
		    for (j = num_remind ; j > i ; j--)
		     strcpy(reminders[j],reminders[j-1]);
		     
		    strcpy(reminders[i],day_str);
		    strcat(reminders[i],msg_str);
		    
		    num_remind++;
		}
		
		printf("\nDay Reminder\n");
		for( i = 0 ; i< num_remind ; i++)
		 printf(" %s\n",reminders[i]);
		 printf("6.PrintMonthReminderList is completed\n\n");
		 break;
    }
	case 7: //InsertSearchUpdatePrintParts
	{
		control();
	}
    }
  }
}


bool is_prime(int n)
{
	int judge=0;
	for(int i=2;i<n;i++)
	{
		if(n % i == 0) judge = 1;
    }
    return judge;
}

int read_line(char str[],int n)
{ 
     int ch,i=0;
     while((ch= getchar()) != '\n')
       if(i < n)
         str[i++] = ch;
     str[i] = '\0';
     return i;
}



void control(void){
	part* head = NULL;
	char ch;
	
	
	while(1){
		printf("请输入i s u p q：");
		getchar();
		ch = getchar (); 
		if(ch != 'i' && ch != 's'&&ch != 'u'&&ch != 'p'&&ch != 'q'){
			printf("输入有误\n");
			continue; 
		}
		switch(ch){
			case 'i':create(&head);break;
			case 's':{
				int num = 0;
				printf("输入部件编号：") ;
				scanf("%d",&num);
				search(head,num);
				break;
				} 
			case 'u':{
				int num = 0;
				printf("输入需要更新的部件编号：") ;
				scanf("%d",&num);
				searchchange(head,num);
				break;
				break;} 
			case 'p':print(head);break;
			case 'q':exit(0);break;
		}
	}
}
void search(part* sc,int aim){
	part* start = sc;
	while(start != NULL){
		if(start->number == aim) break;
		start = start->next;
	}
	if(start != NULL){
		outputSEARCH(start);
	}else printf("未找到");
}


void create(part** newadd){
	part* Part = (part*)malloc(sizeof(part));
	static part* tail;
	
	printf("请输入编号：");
	scanf("%d",&Part->number);
	printf("请输入名称：");
	scanf("%s",Part->name);
	printf("请输入数量：");
	scanf("%d",&Part->quantity);
	
	if(*newadd == NULL){
		*newadd = Part;
		Part->next = NULL;
	}
	else{
		tail->next = Part;
		Part->next = NULL;
	}
	tail = Part;
}


void print(part* pri){
	part* start = pri;
	while(start != NULL){
		printf("\n编号：%d\n",start->number);
		printf("名字：%s\n",start->name);
		printf("数量：%d\n",start->quantity);
		start = start->next;
	}
}




void outputSEARCH(part* print){
	printf("\n编号：%d\n", print->number);
	printf("名字：%s\n", print->name);
	printf("数量：%d\n",print->quantity);
}


void change(part* pri){
	printf("请输入名称：");
	scanf("%s",pri->name);
	printf("请输入数量：");
	scanf("%d",&pri->quantity);
}


void searchchange(part* sc,int aim){
	part* start = sc;
	while(start != NULL){
		if(start->number == aim) break;
		start = start->next;
	}
	if(start != NULL){
		change(start);
	}else printf("未找到");
}






